package com.hoshiicloud.user.controller.store;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.common.exception.ServiceException;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.utils.BeanUtils;
import com.hoshiicloud.user.entity.store.StoreGoodsCategory;
import com.hoshiicloud.user.param.store.StoreGoodsCategoryAddParam;
import com.hoshiicloud.user.param.store.StoreGoodsCategoryQueryParam;
import com.hoshiicloud.user.param.store.StoreGoodsCategoryUpdateParam;
import com.hoshiicloud.user.service.store.StoreGoodsCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiSort;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 店铺经营类目 控制器
 * </p>
 *
 * @author zhangshuhu
 * @since 2019-04-28
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/store/store-goods-category")
@Api(value = "平台店铺经营类目", tags = {"平台店铺经营类目"})
@ApiSort(42)
public class StoreGoodsCategoryController {
    @Autowired
    private StoreGoodsCategoryService storeGoodsCategoryService;

    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 分页查询店铺经营类目
     */
    @ApiOperation(value = "分页查询店铺经营类目")
    @PostMapping("/query/page")
    public Response page(@RequestBody StoreGoodsCategoryQueryParam param) {
        try {
            QueryWrapper<StoreGoodsCategory> queryWrapper = new QueryWrapper<StoreGoodsCategory>();
            queryWrapper.lambda()
                    .eq((param.getStoreId() != null), StoreGoodsCategory::getStoreId, param.getStoreId())
                    .eq((param.getCategoryId() != null), StoreGoodsCategory::getCategoryId, param.getCategoryId())
                    .eq((param.getWeight() != null), StoreGoodsCategory::getWeight, param.getWeight())
                    .eq((param.getOrdered() != null), StoreGoodsCategory::getOrdered, param.getOrdered())
                    .eq((param.getEnabled() != null), StoreGoodsCategory::getEnabled, param.getEnabled());
            IPage<StoreGoodsCategory> page = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));
            String key = param.toString() + ",Page{pageNumber=" + param.getPageNumber() + ",pageSize=" + param.getPageSize() + "}";
            return Response.data(storeGoodsCategoryService.getPage(page, queryWrapper, key));
        } catch (Exception e) {
            log.error("分页查询店铺经营类目异常", e);
            return Response.failed("分页查询店铺经营类目");

        }

    }

    /**
     * @param id
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 根据id查询店铺经营类目
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询店铺经营类目")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response queryById(@PathVariable("id") Long id) {
        return Response.data(storeGoodsCategoryService.getById(id));
    }

    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增店铺经营类目")
    public Response saveStoreGoodsCategory(@RequestBody @Valid StoreGoodsCategoryAddParam param) {
        StoreGoodsCategory entity;
        try {
            entity = BeanUtils.covertType(param, StoreGoodsCategory.class);
        } catch (Exception e) {
            log.error("新增店铺经营类目bean转换异常", e);
            return Response.failed("新增店铺经营类目bean转换异常");
        }
        return Response.data(storeGoodsCategoryService.save(entity));
    }

    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 修改店铺经营类目
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改店铺经营类目")
    public Response updateStoreGoodsCategoryById(@RequestBody @Valid StoreGoodsCategoryUpdateParam param) {
        StoreGoodsCategory entity;
        try {
            entity = BeanUtils.covertType(param, StoreGoodsCategory.class);
        } catch (Exception e) {
            log.error("修改店铺经营类目Bean转换异常", e);
            throw new ServiceException("修改店铺经营类目Bean转换异常");
        }
        return Response.data(storeGoodsCategoryService.updateById(entity));
    }

    /**
     * @Author:campchen
     * @Description: StringUtils.join(ids, " ") 为缓存中添加唯一KEY  如不合适可选择更好方法
     * @Date:上午 10:18 2019/5/11
     * @Param: ids
     * @return: com.hoshiicloud.common.rpc.Response
     */
    @ApiOperation(value = "根据id集合查询")
    @GetMapping("/getByIds")
    public Response getByIds(@RequestParam List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return Response.failed("获取店铺经营类目id集合失败");
        }
        ids.sort(null);
        return Response.data(storeGoodsCategoryService.getByIds(ids, StringUtils.join(ids, "")));
    }
}
